package Math;

import java.util.Arrays;

public class _645_SetMismatch {
    //bruce force solution 1:
    public int[] findErrorNums(int[] nums) {
        Arrays.sort(nums);
        int[] result = new int[2];
        for (int i = 0; i < nums.length-1; i++) {
            if (nums[i]+1!=nums[i+1]) {
                if (nums[i] == nums[i + 1]) {
                    result[0] = nums[i];
                } else {
                    result[1] = (nums[i+1] + nums[i])/2;
                }
            }
        }
        if (nums[0] != 1) {
            result[1] = 1;
        } else if (nums[nums.length - 1] != nums.length) {
            result[1] = nums.length;
        }
        return result;
    }

    //reference solution:using arrays to hold the count
    public int[] findErrorNums1(int[] nums) {
        int[] arr = new int[nums.length + 1];
        int dup = -1, missing = 1;
        for (int i = 0; i < nums.length; i++) {
            arr[nums[i]] += 1;
        }
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] == 0)
                missing = i;
            else if (arr[i] == 2)
                dup = i;
        }
        return new int[]{dup, missing};
    }
}
